package com.example.android.chartapplication.Fragments;


import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.android.chartapplication.ChartCircle;
import com.example.android.chartapplication.R;

import java.util.Random;

public class ChartCircleFragment extends Fragment {

    ChartCircle chartCircle;
    protected View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (null == view) view = inflater.inflate(R.layout.fragment_chart_circle, container, false);
        chartCircle = view.findViewById(R.id.chartCircle);
        return view;
    }

    Handler handler = new Handler();
    Random random = new Random();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            chartCircle.addData(random.nextInt(30));
            handler.postDelayed(this, 1500);
        }
    };

    @Override
    public void onStart() {
        super.onStart();
        handler.postDelayed(runnable, 1500);
    }

    @Override
    public void onPause() {
        super.onPause();
        handler.removeCallbacks(runnable);
    }
}
